<?php defined('BASEPATH') OR exit('No direct script access allowed');

// Code here is run before ALL controllers
class MY_Controller extends CI_Controller
{
    public $version='one';
    public $title = 'placesymbol';
    public $keywords = '地区标志, 地区, 标志, 地区产品标志, 地区产品, 产品标志, 传输, 存储, 同步, 备份, 拍照, 上传, 下载, 中转, 文件, 照片, 相册, 离线, 传文件, place symbol, placesymbol';
    public $description = '';

    public $navigation = '';

    function __construct()
    {
        parent :: __construct();
        $this->load->helper(array('url','form','language'));
        $this->load->library(array('form_validation','session','pagination'));
        $this->lang->load('public', 'chinese');
        $this->load->model(array('Opts','Entries','Tags','Monog'));

        $this->initOptions();
    }

    protected function setHeader($key, $value){
        if(! empty($value))$this->$key = $value;
    }

    protected function getHeader($key){
        return $this->$key;
    }

    protected function initOptions()
    {
        $op = $this->Opts->get_options();
        foreach($op as $item)
        {
            $this->{$item['name']} = $item['value'];
        }
    }

    protected function _isnot_login()
    {
        return $this->session->userdata('uid') ? FALSE : TRUE;
    }

    /** 成功 || 失败页面 */
    protected function goback($flag=1)
    {
        $this->load->view('goback',array('flag'=>$flag));
    }

    /** navigation */
    protected function navigation()
    {
        preg_match('/[a-z]+\/[a-z]+/', $this->uri->ruri_string(), $uriarray);
        $nav[0] = anchor('', '首页', 'title="首页"');
        $nav[1] = str_replace('%title%', $this->getHeader('title'), lang($uriarray[0]));
        $this->navigation = implode('>>', $nav);
    }

    //tags
    protected function tags()
    {
        $this->db->cache_on();
        $otherData['tags'] = $this->Tags->get_tag_entries();
        return $this->load->view('proceed/tags' ,$otherData, true);
    }

    //Monographic
    protected function monographics()
    {
        $this->db->cache_on();
        $parm['order'] = 'order desc';
        $otherData['monographic'] = $this->Monog->getMonographic($parm);
        return $this->load->view('proceed/monographic' ,$otherData, true);
    }
    //hot
    protected function hots()
    {
        $this->db->cache_on();
        $parm['where'] = array(
            'types'=>'topic',
            'm.type'=>'category'
        );
        $parm['order'] = 'order desc';
        $otherData['hots'] = $this->Entries->getTopic($parm);
        return $this->load->view('proceed/hot' ,$otherData, true);
    }
    //Culture
    protected function culture()
    {
        $this->db->cache_on();
        $parm['where'] = array(
            'types'=>'topic',
            'm.type'=>'category',
            'm.name'=>'Culture'
        );
        $otherData['culture'] = $this->Entries->getTopic($parm);
        return $this->load->view('proceed/culture' ,$otherData, true);
    }
    //Policy
    protected function policy()
    {
        $this->db->cache_on();
        $parm['where'] = array(
            'types'=>'topic',
            'm.type'=>'category',
            'm.name'=>'Policy'
        );
        $otherData['policys'] = $this->Entries->getTopic($parm);
        return $this->load->view('proceed/policy' ,$otherData, true);
    }

    //news
    protected function news()
    {
        $this->db->cache_on();
        $parm['where'] = array(
            'types'=>'topic',
            'm.type'=>'category',
            'm.name'=>'news'
        );
        $otherData['news'] = $this->Entries->getTopic($parm);
        return $this->load->view('proceed/news' ,$otherData, true);
    }
    
    // 产品信息
    protected function placesymbols()
    {
        $this->db->cache_on();
        $parm['where'] = array(
            'types'=>'topic',
            'm.type'=>'category'
        );
        $otherData['placesymbols'] = $this->Entries->getTopic($parm, 1, 6);
        return $this->load->view('proceed/placesymbols' ,$otherData, true);
    }
    
    // 交易信息
    protected function merchandises()
    {
        $this->db->cache_on();
        $this->load->model('Merchandise_model');
        $parm['where'] = array();
        $otherData['merchandises'] = $this->Merchandise_model->get_merchandises($parm, 1, 6);
        return $this->load->view('proceed/merchandises' ,$otherData, true);
    }
    
    // 问答
    protected function answers()
    {
        $this->db->cache_on();
        $this->load->model('Answers_model');
        $parm['where'] = array();
        $otherData['answers'] = $this->Answers_model->getAnswers($parm,1,6);
        return $this->load->view('proceed/answers' ,$otherData, true);
    }
}


class MY_Admin extends CI_Controller
{
    public $version;
    private $premissions = array(
        'administrator'=>1,
        'manager'=>1,
        'user'=>0
    );

    function __construct()
    {
        parent :: __construct();
        $this->load->helper(array('url','form','language'));
        $this->load->library(array('form_validation','session', 'pagination','Pscws4'));
        $this->load->model(array('Opts','Entries','Tags','Monog'));
        $this->initOptions();
        $this->version = 123;

        $this->_check_premissions();
    }

    /** 分词 */
    protected function split_words($keyword)
    {
        $this->pscws4->set_dict(APPPATH.'/libraries/scws/dict.utf8.xdb');
        $this->pscws4->set_rule(APPPATH.'/libraries/scws/rules.utf8.ini');
        $this->pscws4->set_ignore(true);
        $this->pscws4->send_text($keyword);
        $words = $this->pscws4->get_tops(10);
        $tags = array();
        foreach ($words as $val) {
            $tags[] = $val['word'];
        }
        $this->pscws4->close();
        return $tags;
    }

    protected function initOptions()
    {
        $op = $this->Opts->get_options();
        foreach($op as $item)
        {
            $this->{$item['name']} = $item['value'];
        }
    }
    //成功 || 失败页面
    protected function goback($flag=1)
    {
        $this->load->view('admins/goback',array('flag'=>$flag));
    }
    protected function _isnot_login()
    {
        return $this->session->userdata('uid') ? FALSE : TRUE;
    }

    protected function _check_premissions()
    {
        if($this->_isnot_login())
        {
            $prem = $this->session->userdata('group');
            if(empty($prem) || !isset($this->premissions[$prem]) || !$this->premissions[$prem]){
                redirect('user');
            }
        }
        return true;
    }
}